package ymbky.firstcodesunnyweather.planbook

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.ContactsContract
import android.text.TextUtils
import android.view.View
import kotlinx.android.synthetic.main.activity_add.*
import ymbky.firstcodesunnyweather.planbook.util.ToastUtil
import java.text.SimpleDateFormat
import java.util.*

class AddActivity : AppCompatActivity() {

    private var RdDbopenHelper: RecordDbOpenHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add)
        RdDbopenHelper = RecordDbOpenHelper(this)

    }

    fun add(view: View) {
        val title = et_title!!.text.toString()
        val affair = et_content!!.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtil.toastShort(this, "标题不能为空")
            return
        }
        val record = Record()
        record.title = title
        record.affair = affair
        record.createTime = currentTimeFormat
        val row = RdDbopenHelper!!.insertData(record)
        if (row != -1L) {
            ToastUtil.toastShort(this, "添加成功！")
            finish()
        } else {
            ToastUtil.toastShort(this, "添加失败！")
        }

    }

    private val currentTimeFormat: String
        @SuppressLint("SimpleDateFormat")
        get() {
            val simpleDataFormat = SimpleDateFormat("yyyy年MM月dd日 HH：mm：ss")
            val date = Date()
            return simpleDataFormat.format(date)
        }


}